<?php
if (!defined('MVC_ROOT_DIR'))  die('Hacking attempt');
class System
{
	private $db;
	private $table;	
	private $tableColumns;
	private $tableKeywords;
	private $tableShip;
	
	private $colPArray;
	private $colCArray=array();
	private $colArray;
	
	
	private $menuArr =array();
	private $menuHtml;
	private $menuUser;
	public function __construct()
	{
		$this->db=new Mysql();
		$this->table = $this->db->pre.'system';
		$this->tableColumns = $this->db->pre.'columns';	
		$this->tableKeywords = $this->db->pre.'keywords';	
		$this->tableShip = $this->db->pre.'ship';
		
		
	}
	public function getMenuList($uid)
	{
		$this->menuUser = $this->getUserinfo($uid);
		$sql = "SELECT * FROM ".$this->db->pre."menu WHERE isuse = 1 AND pmodule = 'admin' ORDER BY `sort` ";
		$this->menuArr = $this->db->getAll($sql);
		$this->getMenuTr();
		return $this->menuHtml;
		
	}
	public function getUserinfo($uid){
		$sql = "SELECT a.*,b.role FROM ".$this->db->pre."user a LEFT JOIN ".$this->db->pre."user_role b ON a.uid = b.uid WHERE a.uid = $uid";
		return $this->db->getRow($sql);
	}
	public function getMenuTr(){
		
		foreach ($this->menuArr as $key=>$value) {
			if ($value['pid']==0){
				$checked='';
				$userMenu = $this->menuUser['role'];
				$menuArr = explode(',', $userMenu);
				if (in_array($value['id'], $menuArr)){
					$checked = 'checked="checked"';
				}else{
					if ($this->menuHaveChild($menuArr,$value['id'])){
							$checked = 'checked="checked"';	
					}
				}
				$this->menuHtml.='<tr><td><input type="checkbox" class="checkitem" value="'.$value['id'].'" '.$checked.'> '.$value['name'].'</td></tr>';				
				$this->getMenuChildTr($value['id'],'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;');
			}
		}
	}
	
	public function menuHaveChild($menuArr,$pid)
	{
		foreach ($menuArr as $key=>$value) {
			$va=array();
			foreach ($this->menuArr as $k=>$v){
				
				if ($value == $v['id']){
					$va = $v;
					break;
				}
			}
			if ($va){
				if ($va['pid']==$pid) return true;
			}
		}
		return false;
	}
	public function getMenuChildTr($pid,$dist){
		foreach ($this->menuArr as $key=>$value)
		{
			if ($value['pid']==$pid)
			{				
				$checked='';
				$userMenu = $this->menuUser['role'];
				$menuArr = explode(',', $userMenu);
				if (in_array($value['id'], $menuArr)){
					$checked = 'checked="checked"';
				}else{
					if ($this->menuHaveChild($menuArr,$value['id'])){
							$checked = 'checked="checked"';	
					}
				}
				$this->menuHtml.='<tr><td>'.$dist.'<input type="checkbox" class="checkitem" value="'.$value['id'].'" '.$checked.'> '.$value['name'].'</td></tr>';				
				$this->getMenuChildTr($value['id'],$dist.'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;');
			}
		}
	}
	
	public function getColumnsList()
	{
		$arr=array();
		$sql = "SELECT * FROM ".$this->tableColumns." WHERE 1";
		$col = $this->db->getAll($sql);		$parr=array();
		foreach ($col as $k=>$v)
		{
			if ($v['pid']==0) 
			{
				$parr[] = $v;
			}else{
				$carr[] = $v;
			}
		}		
		$i=0;
		if ($parr ){
			foreach ($parr as $key=>$value)
			{
				$arr[$i] = $value;                if (@$carr){
    				foreach ($carr as $k=>$v)    
    				{    
    					if ($v['pid']==$value['id']){    
    						$arr[$i]['child'][]=$v;    
    					}    
    				}                }
				$i++;
			}
		}
		return $arr;		
	}
	
	public function addColumns($data)
	{
		
		return $this->db->insert($this->tableColumns, $data);
	}
	public function modifyColumns($id,$data)
	{
		return $this->db->updateData($this->tableColumns, $data, "WHERE id=".$id);
	}
	public function delColumns($id)
	{
		$sql = "DELETE FROM ".$this->tableColumns." WHERE id = '$id' LIMIT 1";
		return $this->db->delete($sql);
	}
	public function getInfo($keys)
	{
		$sql = "SELECT *  FROM ".$this->table." WHERE key1 like '$keys' LIMIT 1";
		return $this->db->getRow($sql);
	}
	public function saveInfo($keys,$info){
		if ($this->getInfo($keys)){
			$sql = "UPDATE ".$this->table." SET `value1` = '$info' WHERE key1 like '$keys' LIMIT 1 ";
			return $this->db->update($sql);
		}else{			
			$data = array('key1'=>$keys,'value1'=>$info);
			return $this->db->insert($this->table,$data);
		}
		
	}
	public function getKeywords()
	{
		$sql = "SELECT * FROM ".$this->tableKeywords." WHERE 1 ";
		$name = Http::get('name');
		if(!empty($name)){
			$sql = $sql." AND name like '%".$name."%' ";
		}
		
		$page = new Page();		
		$page->uriStyle ='?';
		$list = $page->getPage($sql);
		$html = $page->htmlPage();
		return array('list'=>$list,'html'=>$html);
	}
	public function delKeywords($id)
	{
		$sql = "DELETE FROM ".$this->tableKeywords." WHERE id in (".$id.")";
		return $this->db->delete($sql);
	}
	
	public function updateKeywords($id,$name)
	{
		$sql = "UPDATE ".$this->tableKeywords." SET name = '$name' WHERE id='$id' LIMIT 1";
		
		return $this->db->update($sql);
	}
	public function addKeywors($data)
	{
		return $this->db->insert($this->tableKeywords, $data);
	}
	public function getGlobal(){
		$sql = "SELECT * FROM ".$this->table." WHERE key1 like 'globalinfo'";
		return $this->db->getRow($sql);
	}
	public function saveGlobal($data)
	{
		$array = array('value1'=>serialize($data));
		if ($this->getGlobal()){
			return $this->db->updateData($this->table, $array, " WHERE key1 like 'globalinfo' ");
		}else{
			$array['key1']='globalinfo';
			return $this->db->insert($this->table, $array);
		}
	}
	public function getNewsList()
	{
		$sql = "SELECT * FROM ".$this->db->pre."news  WHERE `status`=1 ORDER BY updateTime DESC";
		return  $this->db->getAll($sql);
		
	}
	public function getGoodsList()
	{
		$sql = "SELECT * FROM ".$this->db->pre."goods  WHERE `is_on`=1 ORDER BY last_update DESC";
		return  $this->db->getAll($sql);
	}
	public function createSitemap($type='baidu')
	{
		$dom = new DOMDocument('1.0','utf-8');
		// we want a nice output
		$dom->formatOutput = true;
		$root = $dom->createElement('sitemapindex');
		$root = $dom->appendChild($root);
		$rootAttr  = $dom->createAttribute("xmlns");
		$root->appendChild($rootAttr);
		$rootAttrValue = $dom->createTextNode("http://www.sitemaps.org/schemas/sitemap/0.9");
		$rootAttr->appendChild($rootAttrValue);
		
		$sitemap = $dom->createElement('sitemap');
		$sitemap = $root->appendChild($sitemap);
			
		$loc = $dom->createElement('loc');
		$loc = $sitemap->appendChild($loc);
		$locValue = $dom->createTextNode(MVC_WWW_HOST.'sitemap_'.$type.'_list.xml');
		$loc->appendChild($locValue);
		
		$lastmod = $dom->createElement('lastmod');
		$lastmod = $sitemap->appendChild($lastmod);
		$lastmodValue = $dom->createTextNode(date("Y-m-d"));
		$lastmod->appendChild($lastmodValue);
		$indexXml = $dom->saveXML();
		//echo $indexXml;
		Files::writeHtml('', 'sitemap_'.$type.'.xml', $indexXml);
		
	}
	public function createSitemapList($type){
		$dom = new DOMDocument('1.0','utf-8');
		// we want a nice output
		$dom->formatOutput = true;
		
		$root = $dom->createElement('urlset');
		$root = $dom->appendChild($root);
		$rootAttr  = $dom->createAttribute("xmlns");
		$root->appendChild($rootAttr);
		$rootAttrValue = $dom->createTextNode("http://www.sitemaps.org/schemas/sitemap/0.9");
		$rootAttr->appendChild($rootAttrValue);
		
		$columns = $this->getColumnsList();
		$newsList = $this->getNewslist();
		$goodsList = $this->getGoodsList();
		$url = $dom->createElement('url');
		$url = $root->appendChild($url);
		
		$loc = $dom->createElement('loc');
		$loc = $url->appendChild($loc);
		$locValue = $dom->createTextNode(MVC_WWW_HOST.'index.html');
		$loc->appendChild($locValue);
		
		$loc = $dom->createElement('lastmod');
		$loc = $url->appendChild($loc);
		$locValue = $dom->createTextNode(date("Y-m-d"));
		$loc->appendChild($locValue);
		
		$loc = $dom->createElement('changefreq');
		$loc = $url->appendChild($loc);
		$locValue = $dom->createTextNode('daily');
		$loc->appendChild($locValue);
		
		$loc = $dom->createElement('priority');
		$loc = $url->appendChild($loc);
		$locValue = $dom->createTextNode('0.6');
		$loc->appendChild($locValue);
		
		foreach ($columns as $k=>$v){
			$url = $dom->createElement('url');
			$url = $root->appendChild($url);
			
			$loc = $dom->createElement('loc');
			$loc = $url->appendChild($loc);
			$locValue = $dom->createTextNode(MVC_WWW_HOST.$v['ename'].'/index.html');
			$loc->appendChild($locValue);
			
			$loc = $dom->createElement('lastmod');
			$loc = $url->appendChild($loc);
			$locValue = $dom->createTextNode(date("Y-m-d"));
			$loc->appendChild($locValue);
			
			$loc = $dom->createElement('changefreq');
			$loc = $url->appendChild($loc);
			$locValue = $dom->createTextNode('daily');
			$loc->appendChild($locValue);
			
			$loc = $dom->createElement('priority');
			$loc = $url->appendChild($loc);
			$locValue = $dom->createTextNode('0.6');
			$loc->appendChild($locValue);
//			if (@$v['child']){
//				foreach ($v['child'] as $ks=>$vs)
//				{
//					$url = $dom->createElement('url');
//					$url = $root->appendChild($url);
//					
//					$loc = $dom->createElement('loc');
//					$loc = $url->appendChild($loc);
//					$locValue = $dom->createTextNode(MVC_WWW_HOST.$v['ename'].'/'.$vs['ename'].'/index.html');
//					$loc->appendChild($locValue);
//					
//					$loc = $dom->createElement('lastmod');
//					$loc = $url->appendChild($loc);
//					$locValue = $dom->createTextNode(date("Y-m-d"));
//					$loc->appendChild($locValue);
//					
//					$loc = $dom->createElement('changefreq');
//					$loc = $url->appendChild($loc);
//					$locValue = $dom->createTextNode('daily');
//					$loc->appendChild($locValue);
//					
//					$loc = $dom->createElement('priority');
//					$loc = $url->appendChild($loc);
//					$locValue = $dom->createTextNode('0.6');
//					$loc->appendChild($locValue);
//				}
//			}
		}
		
		foreach ($goodsList as $k=>$v)
		{
			$url = $dom->createElement('url');
			$url = $root->appendChild($url);
			
			$loc = $dom->createElement('loc');
			$loc = $url->appendChild($loc);
			$locValue = $dom->createTextNode(MVC_WWW_HOST.'product/info/id/'.$v['goods_id'].'.html');
			$loc->appendChild($locValue);
			
			$loc = $dom->createElement('lastmod');
			$loc = $url->appendChild($loc);
			$locValue = $dom->createTextNode(date("Y-m-d",$v['last_update']));
			$loc->appendChild($locValue);
			
			$loc = $dom->createElement('changefreq');
			$loc = $url->appendChild($loc);
			$locValue = $dom->createTextNode('daily');
			$loc->appendChild($locValue);
			
			$loc = $dom->createElement('priority');
			$loc = $url->appendChild($loc);
			$locValue = $dom->createTextNode('0.6');
			$loc->appendChild($locValue);
		
		}
		
		foreach ($newsList as $k=>$v){
			$url = $dom->createElement('url');
			$url = $root->appendChild($url);
			
			$loc = $dom->createElement('loc');
			$loc = $url->appendChild($loc);
			$locValue = $dom->createTextNode($v['url']);
			$loc->appendChild($locValue);
			
			$loc = $dom->createElement('lastmod');
			$loc = $url->appendChild($loc);
			$locValue = $dom->createTextNode(date("Y-m-d",$v['updateTime']));
			$loc->appendChild($locValue);
			
			$loc = $dom->createElement('changefreq');
			$loc = $url->appendChild($loc);
			$locValue = $dom->createTextNode('daily');
			$loc->appendChild($locValue);
			
			$loc = $dom->createElement('priority');
			$loc = $url->appendChild($loc);
			$locValue = $dom->createTextNode('0.6');
			$loc->appendChild($locValue);
		}
		
		
		$listXml = $dom->saveXML();
		Files::writeHtml('', 'sitemap_'.$type.'_list.xml', $listXml);
	}
	public function getShipList()
	{
		$sql = "SELECT * FROM ".$this->tableShip." ORDER BY ship_id DESC";
		return $this->db->getAll($sql);
	}
	public function getShipPirce($shipId)
	{
		$sql = "SELECT ship_price FROM ".$this->tableShip." WHERE ship_id = '$shipId'";
		return $this->db->getOne($sql);
	}
	public function saveRole($uid,$role)
	{
		return $this->db->updateData($this->db->pre."user_role", array('role'=>$role), "WHERE uid = '$uid'");
				
	}
	public function addRole($data)
	{
		return $this->db->insert($this->db->pre."user", $data,true);
	}
	public function addUserRole($uid)
	{
		$sql = "SELECT id FROM ".$this->db->pre."menu WHERE module = 'home' ";
		$rs = $this->db->getAll($sql);
		$id = '';
		foreach ($rs as $key => $value) {
			if ($id){
				$id = $id.','.$value['id'];
			}else {
				$id = $value['id'];
			}
		}
		$this->db->insert($this->db->pre."user_role", array('uid'=>$uid,'role'=>$id));
	}
	public function getUserInfoByUsername($username)
	{
		$sql= "SELECT * FROM ".$this->db->pre."user WHERE username like '$username'";
		return $this->db->getRow($sql);
	}
	public function getUserInfoByEmail($email)
	{
		$sql= "SELECT * FROM ".$this->db->pre."user WHERE email like '$email'";
		return $this->db->getRow($sql);
	}
	public function delUser($uid)
	{
		
		$sql = "DELETE FROM ".$this->db->pre."user WHERE uid in($uid)";
		return $this->db->delete($sql);
	}
	
	public function dealError()
	{
		$sql = "SELECT  * from ".$this->db->pre."goods WHERE   1	";
		$all = $this->db->getAll($sql);
		foreach ($all as $k=>$v){
			$rs = str_replace('<span class="headlink">', '<span>', $v['goods_desc']);
			$rs2 = str_replace('88888888', '33048111',$rs);
			$rs3 = str_replace('0755-88888889', '', $rs2);
			$rs4 = str_replace('传真：', '', $rs3);
			
			if ($v['goods_desc']!=$rs4){
				$rs4 = str_replace("'","\'",$rs4);
				$sql = "UPDATE ".$this->db->pre."goods  set goods_desc = '$rs4'  WHERE  goods_id =  '".$v['goods_id']."'";
				echo $sql;
				$this->db->query($sql);
				
	die();
			}
		}
	}
}



